<!---->
<template>
    <div class="page_addproduct">
        <common-header :title="'添加产品'">
            <span slot="overwrite-left"></span>
        </common-header>
        <div class="btn-save" @click="saveInfo">保存</div>
        <div class="pages-main" style="height: calc( 100vh - 45px)">
            <div class="item-info">
                <div class="item-item">
                    <x-input title="产品名称" v-model="project.name" placeholder="请输入产品名称"></x-input>
                </div>
                <div class="item-item item-inline">
                    <x-input title="产量" v-model="project.yield" placeholder="请输入产量"
                             style="width: 65%;display:inline-block;"></x-input>
                    <select v-model="project.unitType" class="translateBlock" style="width: 19%;font-size: 0.38rem;">
                        <option v-for="item in list" :value="item.value">{{item.label}}</option>
                    </select>
                </div>
                <div class="item-item item-inline">
                    <x-input title="产值" v-model="project.outputvalue"
                             placeholder="请输入产值" style="width: 65%;display:inline-block;"></x-input>
                    <select v-model="unitTypeText" class="translateBlock" style="width: 19%;font-size: 0.38rem;">
                        <option :value="'万元'">万元</option>
                    </select>
                </div>
                <div class="item-item item-inline">
                    <x-input title="销售" v-model="project.sale" placeholder="请输入销售量"
                             style="width: 65%;display:inline-block;"></x-input>
                    <select v-model="project.saleType" class="translateBlock" style="width: 19%;font-size: 0.38rem;">
                        <option v-for="item in list" :value="item.value">{{item.label}}</option>
                    </select>
                </div>
            </div>

        </div>
    </div>
</template>

<script>
    import {XInput, Selector} from 'vux'
    import company from '../../services/company'

    export default {

        name: "addproduct",
        components: {
            XInput,
            Selector,
        },
        data() {
            return {
                isShowDeleteDialog: false,
                productid: this.$route.query.productid,
                enterpriseid: this.$route.query.enterpriseid,
                unitTypeText: '万元',
                project: {
                    id: '',
                    enterpriseid: '',
                    name: '',
                    yield: '',
                    outputvalue: '',
                    unitType: '',
                    sale: '',
                    saleType: '',
                },
                list: [],
            }
        },

        mounted() {
            this.optionbytypecode()
            if (this.productid) {
                this.productDetail()
            }
        },
        watch: {},
        methods: {
            // 获取单位
            optionbytypecode() {
                let _param = this.$tools.invokeUAF('sys/optionbytypecode', {typecode: 'UNIT_TYPE'})
                company.optionbytypecode(_param).then(res => {
                    let data = res.data.data;
                    this.list = data;
                    this.project.unitType = data[0].value;
                    this.project.saleType = data[0].value;
                })
            },

            // 获取产品详情
            productDetail() {
                let _param = this.$tools.invokeUAF('enterprise/productDetail', {productid: this.productid})
                company.productDetail(_param).then(res => {
                    let data = res.data.data;
                    // this.project = data;
                    let param = {
                        id: '',
                        name: '',
                        yield: '',
                        outputvalue: '',
                        unitType: '',
                        sale: '',
                        saleType: '',
                    }
                    for (let i in param) {
                        this.project[i] = data[i];
                    }

                })
            },

            // 保存产品信息
            saveInfo() {

                let param = {
                    name: '请输入产品名称',
                    yield: '请输入产量',
                    outputvalue: '请输入产值',
                    sale: '请输入销售量',
                }

                for (let i in param) {
                    if (!this.project[i]) {
                        this.$vux.toast.show({
                            text: param[i],
                            type: 'text',
                            position: 'middle',
                        })
                        return;
                    }
                }

                this.project.enterpriseid = this.enterpriseid;
                let _param = this.$tools.invokeUAF('enterprise/editProduct', this.project)
                company.editProduct(_param).then(res => {
                    if(res.data.code === '0'){
                        this.$tools.goPage_query('companyDetail', {id: this.enterpriseid})
                    }

                })
            },
        },
    };
</script>

<style lang="less" rel="stylesheet/less" type="text/css">
    .page_addproduct {
    .item-inline {
    .weui-cell__hd, .weui-cell__bd, .weui-cell__ft {
        display: inline-block !important;
    }
    .weui-cell__bd {
        width: 5rem !important;
    }
    }

    }
</style>
